package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings({"all"})
@Controller
public class FileController {

    /**
     * 打开文件上传请求页面
     *
     * @return 指向JSP的字符串
     */
    @GetMapping("/upload/page")
    public String uploadPage() {
        return "upload.html";
    }

    @PostMapping("/upload/multipart")
    @ResponseBody
    public Map<String, Object> uplod(@RequestParam("photo") MultipartFile photo) {
        String path = "d:/uploaded";
        String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String suffix = photo.getOriginalFilename()
                .substring(photo.getOriginalFilename().lastIndexOf("."));
        if (!suffix.equals(".jpg")) {
            return dealResultMap(false, "上传失败");
        }
        try {
            FileCopyUtils.copy(photo.getInputStream(), new
                    FileOutputStream(path + fileName + suffix));
        } catch (IOException e) {
            e.printStackTrace();
            return dealResultMap(false, "上传失败");
        }
        return dealResultMap(true, "上传成功");
    }

    // 处理上传文件结果
    private Map<String, Object> dealResultMap(boolean success, String msg) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("success", success);
        result.put("msg", msg);
        return result;
    }

    /**
     * 打开文件上传请求页面
     *
     * @return 指向JSP的字符串
     */
    @GetMapping("/upload/page1")
    public String uploadPage1() {
        return "/upload1.html";
    }

    @PostMapping("/upload/multipart1")
    @ResponseBody
    public Map<String, Object> uplod1(@RequestParam("photo") MultipartFile[] photos) {
        String path = "d:/uploaded/";
        System.out.println(photos.length);

        for (MultipartFile photo : photos) {
            if (!photo.isEmpty()) {
                String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
                String suffix = photo.getOriginalFilename()
                        .substring(photo.getOriginalFilename().lastIndexOf("."));
                if (!suffix.equals(".jpg")) {
                    return dealResultMap(false, "上传失败");
                }
                try {
                    FileCopyUtils.copy(photo.getInputStream(), new
                            FileOutputStream(path + fileName + suffix));
                } catch (IOException e) {
                    e.printStackTrace();
                    return dealResultMap(false, "上传失败");
                }
            }
        }
        return dealResultMap(true, "上传成功");
    }

}
